package cn.com.css.taxInspection.workflow.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.com.css.taxInspection.common.result.DataResult;

import cn.com.css.taxInspection.workflow.entity.WorkflowThresholdEntity;
import cn.com.css.taxInspection.workflow.service.WorkflowThresholdDetailService;
import cn.com.css.taxInspection.workflow.service.WorkflowThresholdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 阈值管理
 *
 * @author wenbin
 * @version V1.0
 * @date 2020年3月18日
 */
@Api(tags = "阈值管理")
@RestController
@RequestMapping("/workflow/threshold")
public class WorkflowThresholdController {
    @Resource
    private WorkflowThresholdService workflowThresholdService;
    @Resource
    private WorkflowThresholdDetailService workflowThresholdDetailService;


    @ApiOperation(value = "新增")
    @PostMapping("/add")
    @RequiresPermissions("workflow:threshold:add")
    public DataResult add(@RequestBody WorkflowThresholdEntity sysDict) {
        if (StringUtils.isEmpty(sysDict.getName())) {
            return DataResult.fail("阈值名称不能为空");
        }
        WorkflowThresholdEntity q = workflowThresholdService.getOne(Wrappers.<WorkflowThresholdEntity>lambdaQuery().eq(WorkflowThresholdEntity::getName, sysDict.getName()));
        if (q != null) {
            return DataResult.fail("阈值名称已存在");
        }
        workflowThresholdService.save(sysDict);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("/update")
    @RequiresPermissions("workflow:threshold:update")
    public DataResult update(@RequestBody WorkflowThresholdEntity sysDict) {
        if (StringUtils.isEmpty(sysDict.getName())) {
            return DataResult.fail("阈值名称不能为空");
        }

        WorkflowThresholdEntity q = workflowThresholdService.getOne(Wrappers.<WorkflowThresholdEntity>lambdaQuery().eq(WorkflowThresholdEntity::getName, sysDict.getName()));
        if (q != null && !q.getId().equals(sysDict.getId())) {
            return DataResult.fail("阈值名称已存在");
        }

        workflowThresholdService.updateById(sysDict);
        return DataResult.success();
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "查询分页数据")
    @PostMapping("/listByPage")
    @RequiresPermissions("workflow:threshold:list")
    public DataResult findListByPage(@RequestBody WorkflowThresholdEntity sysDict) {
        Page page = new Page(sysDict.getPage(), sysDict.getLimit());
        LambdaQueryWrapper<WorkflowThresholdEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        if (!StringUtils.isEmpty(sysDict.getName())) {
            queryWrapper.like(WorkflowThresholdEntity::getName, sysDict.getName());
            queryWrapper.or();
            queryWrapper.like(WorkflowThresholdEntity::getRemark, sysDict.getName());
        }
        queryWrapper.orderByAsc(WorkflowThresholdEntity::getName);
        IPage<WorkflowThresholdEntity> iPage = workflowThresholdService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

}
